<template>
  <div class="common-list" :class="{half:column===2}">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="handleLoad"
      :immediate-check="false"
    >

      <div class="list-contanier">
        <router-link
          v-for="item in proList" :key="item.proid"
          custom
          v-slot="{navigate}"
          :to="{name:'detail',params: { proid: item.proid }}"
          >
            <div @click="navigate" class="common-list__item">
            <img class="img" :src="item.img1" />
              <div class="info">
                <p class="proname van-multi-ellipsis--l2">{{ item.proname }}</p>
                <p class="price">
                  <span>{{ item.originprice * item.discount / 10 }}</span>
                  <span>{{ item.originprice }} / <van-tag color="#7232dd" plain>{{ item.discount }} 折</van-tag></span>
                </p>
              </div>
            </div>
        </router-link>
          <van-empty v-if="findNothing" description="抱歉，暂未找到相关的商品" />
      </div>
    </van-list>
  </div>
</template>
<script setup lang="ts">
  import {getProListApi} from '@/api/pro'
  import type {IpageParams} from '@/api/pro'
  import type {IProItem} from '@/@types/pro'
  import {reactive,onMounted,ref,defineProps,watch} from 'vue'
export type column=1|2
export interface IProps{
  action(params:IpageParams):any,
  column?:column
}
const {column=1,action} =defineProps<IProps>()
  const pageInfo=reactive<IpageParams>({
    count:1,
    limitNum:10
  })
const findNothing=ref<boolean>(false)

  const loading=ref<boolean>(false)
  const finished=ref<boolean>(false)
  const proList=ref<IProItem[]>([])
  const handleLoad=async()=>{
    findNothing.value=false
    if(!action)return false
      try {
        const data=await action(pageInfo)
          proList.value=proList.value.concat((data as unknown) as IProItem[])
          loading.value=false
          if(((data as unknown) as IProItem[]).length===0){
            return finished.value=true
          }
          pageInfo.count++
     } catch (error:any) {
        // console.log(typeof error) object
        finished.value=true
        loading.value=false
         // 根据后端返回的数据 单独做一个处理
        if(!proList.value.length)findNothing.value=true
        // if((error.toString().replace('Error: ',''))==='10002'){
        //   findNothing.value=true
        //   loading.value=true
        // }
      }
  }
  const refresh=()=>{
    pageInfo.count=1,
    pageInfo.limitNum=10,
    finished.value=false
    proList.value=[]
    handleLoad()
  }
  watch(()=>action,()=>{
    refresh()
  })
  onMounted(()=>{
    handleLoad()
  })
</script>
<style lang="scss">
.common-list {
  padding-bottom: 80px;
  padding: 10px;
  &.half .list-contanier {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .common-list__item {
      width: 3.4rem;
      flex-direction: column;
      img {
        width: 3.4rem;
        height: 3.4rem;
      }
      .info {
        padding: 10px 0;
      }
    }
  }
  .common-list__item {
    display: flex;
    flex-direction: row;
    margin-bottom: 10px;
    img {
      width: 2rem;
      height: 2rem;
    }
    .info {
      padding-left: 10px;
      padding-bottom: 10px;
      display: flex;
      flex-direction: column;
      /* justify-content: space-between; */
      .proname {
        font-size: 16px;
        margin-bottom: 10px;
      }
      .price {
        span {
          display: inline-block;
          &:first-of-type {
            font-size: 16px;
            font-weight: bold;
            margin-right: 5px;
          }
          &:last-of-type {
            font-size: 12px;
            color: rgb(137, 137, 137);
          }
        }
      }
    }
  }
}
</style>